package com.fingard.dsp.bank.directbank.shie01;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;
import com.shie.openapi.dto.ResponseDto;

public class SHIE01Balance extends SHIE01Base implements IBalance {
	@Override
	public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
		RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
		retAts.writeStart();
		
		RetAtsBalDetail tmpRetDetail = null;
		try {
			while (p_req7506.readRecord()) {
				tmpRetDetail = new RetAtsBalDetail();
				tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
				tmpRetDetail.actArea = p_req7506.readedDetail.actArea;
				tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
				
				ActSetItem actItem = getBankConfig().getActSetItem(p_req7506.readedDetail.actNum, "*");
				if (actItem == null || !FileHelper.existFile(actItem.ownKeyStorePath)) {
					retAts.writeRespCode("-1");
					if (actItem == null) {
						retAts.writeRespInfo("未找到账号[" + p_req7506.readedDetail.actNum + "]商户配置信息");
					} else {
						retAts.writeRespInfo("账号[" + p_req7506.readedDetail.actNum + "]证书文件不存在");
					}
					return retAts;
				}
				
				String serviceCode = "account.balanceQuery";

				Map<String, Object> bizContent = new HashMap<String, Object>();
				
				// 一户通账号
				bizContent.put("custId", p_req7506.readedDetail.actNum);
				// 账户类型
				bizContent.put("accountType", getAccountType());
				
				ResponseDto responseDto = sendToSHIE(actItem, serviceCode, bizContent);
				
				// 解析银行返回
				String tmpRespCode = responseDto.getResponseCode();
				if ("0000".equals(tmpRespCode)) {
					JSONObject respBizContent = JSON.parseObject(responseDto.getBizContent());
					
					String msgCode =  respBizContent.getString("msgCode");
					if ("0000".equals(msgCode)) {
						tmpRetDetail.respCode = "0";
						tmpRetDetail.respInfo = respBizContent.getString("msgInfo");
						tmpRetDetail.currBal = StringHelper.amountDivide100(respBizContent.getString("totalBalance"));
						tmpRetDetail.availBal = StringHelper.amountDivide100(respBizContent.getString("balance"));
						tmpRetDetail.frozenBal = StringHelper.amountDivide100(respBizContent.getString("waitPayAmt"));
					} else {
						tmpRetDetail.respCode = "-1";
						tmpRetDetail.respInfo = respBizContent.getString("msgInfo");
					}
				} else {
					if ("9105".equals(tmpRespCode)) {
						tmpRetDetail.respCode = "0";
					} else {
						tmpRetDetail.respCode = "-1";
					}
					
					tmpRetDetail.respInfo = responseDto.getResponseDesc();
				}
				
				retAts.writeRetDetail(tmpRetDetail, false);
			}
		} catch (Exception e) {
			retAts.writeRespCode("-1");
			retAts.writeRespInfo(e.getMessage());
			e.printStackTrace();
		}
		
		retAts.writeEnd();
		return retAts;
	}

	@Override
	public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
		return null;
	}
}
